home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / Python 133 68K / Demo / pdist / security.py < prev    next >
Text File  |  1996-05-20  |  862b  |  34 lines

  1. class Security:
  2.  
  3.     def __init__(self):
  4.         import os
  5.         env = os.environ
  6.         if env.has_key('PYTHON_KEYFILE'):
  7.             keyfile = env['PYTHON_KEYFILE']
  8.         else:
  9.             keyfile = '.python_keyfile'
  10.             if env.has_key('HOME'):
  11.                 keyfile = os.path.join(env['HOME'], keyfile)
  12.             if not os.path.exists(keyfile):
  13.                 import sys
  14.                 for dir in sys.path:
  15.                     kf = os.path.join(dir, keyfile)
  16.                     if os.path.exists(kf):
  17.                         keyfile = kf
  18.                         break
  19.         try:
  20.             self._key = eval(open(keyfile).readline())
  21.         except IOError:
  22.             raise IOError, "python keyfile %s: cannot open" % keyfile
  23.  
  24.     def _generate_challenge(self):
  25.         import whrandom
  26.         return whrandom.randint(100, 100000)
  27.  
  28.     def _compare_challenge_response(self, challenge, response):
  29.         return self._encode_challenge(challenge) == response
  30.  
  31.     def _encode_challenge(self, challenge):
  32.         p, m = self._key
  33.         return pow(long(challenge), p, m)
  34.